package com.bdmc.alarm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.alarm.domain.AlarmRuleConfig;
import com.bdmc.alarm.domain.bo.AlarmRuleConfigBo;
import com.bdmc.alarm.service.IAlarmRuleConfigService;
import com.bdmc.common.annotation.DataColumn;
import com.bdmc.common.annotation.DataPermission;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.exception.ServiceException;
import com.bdmc.task.domain.TaskWorkPlan;
import com.bdmc.task.service.ITaskWorkPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.alarm.domain.bo.AlarmRuleBo;
import com.bdmc.alarm.domain.vo.AlarmRuleVo;
import com.bdmc.alarm.domain.AlarmRule;
import com.bdmc.alarm.mapper.AlarmRuleMapper;
import com.bdmc.alarm.service.IAlarmRuleService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 报警规则Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-06
 */
@Service
public class AlarmRuleServiceImpl extends ServicePlusImpl<AlarmRuleMapper, AlarmRule, AlarmRuleVo> implements IAlarmRuleService {

    @Autowired
    IAlarmRuleConfigService ruleConfigService;

    @Autowired
    ITaskWorkPlanService workPlanService;

    @Override
    public AlarmRuleVo queryById(Integer alarmRuleId){
        return getVoById(alarmRuleId);
    }

    @Override
    public TableDataInfo<AlarmRuleVo> queryPageList(AlarmRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AlarmRule> lqw = buildQueryWrapper(bo);
        Page<AlarmRuleVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<AlarmRuleVo> mySelectPage(AlarmRuleBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<AlarmRuleVo> queryList(AlarmRuleBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<AlarmRule> buildQueryWrapper(AlarmRuleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AlarmRule> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAlarmRuleName()), AlarmRule::getAlarmRuleName, bo.getAlarmRuleName());
        lqw.eq(bo.getEnabled() != null, AlarmRule::getEnabled, bo.getEnabled());
        return lqw;
    }

    @Transactional
    @Override
    public Boolean insertByBo(AlarmRuleBo bo) {
        AlarmRule add = BeanUtil.toBean(bo, AlarmRule.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setAlarmRuleId(add.getAlarmRuleId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(AlarmRuleBo bo) {
        AlarmRule update = BeanUtil.toBean(bo, AlarmRule.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(AlarmRule entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Transactional
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id : ids) {
            long count = workPlanService.count(new LambdaQueryWrapper<TaskWorkPlan>().eq(TaskWorkPlan::getAlarmRuleId, id));
            if(count>0){
                throw  new ServiceException("该规则已用于排班计划，无法删除");
            }
        }
        return removeByIds(ids);
    }
}
